package adventure.commands;

import adventure.*;
import adventure.misc.Currency;
import adventure.placeable.Item;
import colour.ColourUtil;
import player.Player;

/**
 *
 * @author Michael Hanns
 *
 */
public class Drop extends AbstractCommand {

    private Currency currency;

    public Drop(Adventure a) {
        super(a, new String[]{"DROP"});
        this.currency = a.getCurrency();
    }

    @Override
    public String validateAndExecute(Player player, String[] args) {
        if (args.length == 1) {
            return "Drop what?";
        } else {
            return drop(player, Util.consolidateArguments(args));
        }
    }

    private String drop(Player player, String object) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        if (currency.isReferencingCurrency(object, false)) {
            return dropCurrency(player, x, y, z, object);
        } else {
            Item item = player.getInventoryItem(object);

            if (item.getID() > 0) {

                if (item.isBound()) {
                    return ColourUtil.colourise(item.getName(), ColourUtil.item)
                            + " is bound to you!";
                } else {
                    player.removeInventoryItem(object);
                    rooms.addItemToRoom(item, x, y, z);

                    // Other players see depending on light situation
                    if (((AdventureEdit) adv).getRoomManager().isLightPresent(
                            player.x(), player.y(), player.z())) {
                        out.broadcastLocalToPlayer(player,
                                ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                                + " has dropped "
                                + ColourUtil.colourise(item.getName(), ColourUtil.cyan)
                                + ".");
                    } else {
                        out.broadcastLocalToPlayer(player,
                                "Someone has dropped something.");
                    }

                    return "You drop "
                            + ColourUtil.colourise(item.getName(), ColourUtil.cyan)
                            + ".";
                }
            } else {
                return "You don't have any " + object + "!";
            }
        }
    }

    private String dropCurrency(Player player, int x, int y, int z, String object) {
        String[] tokens = object.split(" ");

        if (tokens.length == 1) {
            return dropAmount(player, player.getCurrencyAmount(), x, y, z);
        } else if (tokens.length == 2 && currency.isReferencingCurrency(tokens[1], false)) {
            if (Util.isInteger(tokens[0])) {
                return dropAmount(player, Integer.parseInt(tokens[0]), x, y, z);
            } else {
                return "You must specify an amount of " + currency.getStatusID() + ", or simply 'get "
                        + currency + "'.";
            }
        } else if (tokens.length > 2) {
            String tokensJoined = object.substring(tokens[0].length());
            if (Util.isInteger(tokens[0]) && currency.isReferencingCurrency(tokensJoined, false)) {
                return dropAmount(player, Integer.parseInt(tokens[0]), x, y, z);
            } else {
                return "You must specify an amount of " + currency.getStatusID() + ", or simply 'get "
                        + currency + "'.";
            }
        } else {
            return "You must specify an amount of " + currency.getStatusID() + ", or simply 'get "
                    + currency.getPlural() + "'.";
        }
    }

    private String dropAmount(Player player, int amountSpecified, int x, int y, int z) {
        int amount = rooms.dropRoomCurrency(x, y, z, player, amountSpecified);

        if (amount > 0) {
            String currencyName;
            if (amount > 1) {
                currencyName = amount + " " + currency.getPlural();
            } else {
                currencyName = "a " + currency.getSingular();
            }


            // Other players see depending on light situation
            if (((AdventureEdit) adv).getRoomManager().isLightPresent(
                    player.x(), player.y(), player.z())) {
                out.broadcastLocalToPlayer(player,
                        ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                        + " drops "
                        + ColourUtil.colourise(currencyName, ColourUtil.yellow)
                        + ".");
            } else {
                if (amount > 1) {
                    out.broadcastLocalToPlayer(player,
                            "Someone has dropped "
                            + currency.getPluralInDark() + ".");
                } else {
                    out.broadcastLocalToPlayer(player,
                            "Someone has dropped "
                            + currency.getSingularInDark() + ".");
                }
            }


            return "You drop "
                    + ColourUtil.colourise(currencyName, ColourUtil.yellow) + ".";
        } else {
            return "You do not have any " + currency.getStatusID() + " to drop!";
        }
    }
}
